package com.isoftstone.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.isoftstone.pojo.Users;
import com.isoftstone.service.UserService;
@Controller
public class UserControlle {
	

	@Autowired
	private UserService userService;
	
	@RequestMapping("/adduser")
	public String adduser() {
		return "adduser";
	}
	

	//录入人员信息
	@RequestMapping(path = "/register", method = RequestMethod.POST)
	public String register4Post(Users user,HttpServletRequest req,HttpServletResponse resp) {
		user.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		System.out.println("user控制器方法运行");
		int i=userService.register(user);
		if (i>0) {
			return list(req,resp);
		}
		return "error";		
	}
	
	//人员信息查看
	@RequestMapping("/user")
	public String list(HttpServletRequest req,HttpServletResponse resp) {
		List<Users> users=null;
		users=userService.getAllUser();
		req.setAttribute("list", users);
		return "listuser";
	}
	
	@RequestMapping("/selectuser")
	public String selectuser(HttpServletRequest req,HttpServletResponse resp) {
		String idsrc = req.getParameter("id");
		int id = Integer.parseInt(idsrc);
		Users user = userService.selectuser(id);
		req.setAttribute("user", user);
		return "updateuser";
	}
	
	
	//信息修改和删除
	@RequestMapping("/modifyuser")
	public String modifyuser(Users user,HttpServletRequest req,HttpServletResponse resp) {
		String idsrc = req.getParameter("id");
		String action = req.getParameter("action");
		int id = Integer.parseInt(idsrc);
		user.setId(id);
		int re = 0;
		if ("updateuser".equals(action)) {
			re = userService.updateUser(user);
		}else {
			re = userService.deleteUser(id);
		}
		if(re==1) {
			return list(req,resp);
		}else {
			return "error";
		}
	}
}
